<template>
  <div>
    <!-- 导入表 -->
    <el-dialog
      title="导入表"
      :visible.sync="visible"
      :top="scrollTop"
      :modal="false"
      :appendToBody="false"
      width="85%"
      v-dialog-drag
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        @submit.native.prevent
      >
        <el-form-item label="表名称" prop="tableName">
          <el-input
            v-model="queryParams.tableName"
            placeholder="请输入表名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="表描述" prop="tableComment">
          <el-input
            v-model="queryParams.tableComment"
            placeholder="请输入表描述"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-row>
        <el-table
          @row-click="clickRow"
          ref="table"
          stripe
          border
          :data="dbTableList"
          @selection-change="handleSelectionChange"
          height="260px"
        >
          <el-table-column type="selection" width="60"></el-table-column>
          <el-table-column prop="tableName" label="表名称"></el-table-column>
          <el-table-column prop="tableComment" label="表描述"></el-table-column>
          <el-table-column prop="createTime" label="创建时间"></el-table-column>
          <el-table-column prop="updateTime" label="更新时间"></el-table-column>
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleGenInfo">保 存</el-button>
        <el-button @click="visible = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="生成信息"
      :visible.sync="genInfoVisible"
      top="10vh"
      :modal="false"
      :appendToBody="false"
      width="60%"
      v-dialog-drag
      @close="handleCloseGenInfoDialog"
    >
      <el-form
        ref="genInfoForm"
        :model="info"
        :rules="rules"
        label-width="150px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item prop="packageName">
              <span slot="label">
                生成包路径
                <el-tooltip
                  content="生成在哪个java包下，例如 com.ruoyi.system"
                  placement="top"
                >
                  <i class="el-icon-question"></i>
                </el-tooltip>
              </span>
              <el-input v-model="info.packageName" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="moduleName">
              <span slot="label">
                生成模块名
                <el-tooltip
                  content="可理解为子系统名，例如 system"
                  placement="top"
                >
                  <i class="el-icon-question"></i>
                </el-tooltip>
              </span>
              <el-input v-model="info.moduleName" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item prop="businessName">
              <span slot="label">
                生成业务名
                <el-tooltip
                  content="可理解为功能英文名,用于生成rest接口请求路径，例如 user"
                  placement="top"
                >
                  <i class="el-icon-question"></i>
                </el-tooltip>
              </span>
              <el-input v-model="info.businessName" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="serverName">
              <span slot="label"> 微服务名称 </span>
              <el-input
                v-model="info.serverName"
                placeholder="请输入微服务名称"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleImportTable">保 存</el-button>
        <el-button @click="genInfoVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDbTable, importTable } from "@/api/tool/gen";
export default {
  data() {
    return {
      // 遮罩层
      visible: false,
      // 选中数组值
      tables: [],
      // 总条数
      total: 0,
      // 表数据
      dbTableList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tableName: undefined,
        tableComment: undefined,
      },
      scrollTop: null,
      genInfoVisible: false,
      info: {
        packageName: null,
        moduleName: null,
        businessName: null,
        serverName: null,
      },
      rules: {
        packageName: [
          { required: true, message: "请输入生成包路径", trigger: "blur" }
        ],
        moduleName: [
          { required: true, message: "请输入生成模块名", trigger: "blur" }
        ],
        businessName: [
          { required: true, message: "请输入生成业务名", trigger: "blur" }
        ],
      }
    };
  },
  methods: {
    // 显示弹框
    show() {
      this.getList();
      this.visible = true;
    },
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.tables = selection.map((item) => item.tableName);
    },
    // 查询表数据
    getList() {
      listDbTable(this.queryParams).then((res) => {
        if (res.code === 200) {
          this.dbTableList = res.rows;
          this.total = res.total;
        }
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleGenInfo() {
      if (!this.tables?.length) {
        this.$message.warning("请选择导入的表");
        return;
      }
      this.genInfoVisible = true;
    },
    /** 导入按钮操作 */
    handleImportTable() {
      const param = { tables: this.tables.join(","), ...this.info };

      importTable(param).then((res) => {
        this.msgSuccess(res.msg || "操作成功");
        if (res.code === 200) {
          this.genInfoVisible = false;
          this.visible = false;
          this.$emit("ok");
        }
      });
    },
    handleCloseGenInfoDialog() {
      const genInfoForm = this.$refs.genInfoForm
      if (genInfoForm) {
        genInfoForm.resetFields()
      }
    },
    getScroll() {
      var appMain = document.getElementById("app-main");
      this.scrollTop = appMain.scrollTop;
      this.scrollTop += "px";
    },
  },
  mounted() {
    this.utils.mounteds(this);
  },
  destroyed() {
    this.utils.destroyeds(this);
  },
};
</script>
